package com.tinyideatech.admin.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.MallNews;
import com.tinyideatech.system.service.IMallNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-08-29
 */

@Controller
@RequestMapping("/mall/news")
public class MallNewsController extends BaseController {

    @Autowired
    private IMallNewsService mallNewsService;

    @GetMapping()
    public String list() {
        return "mall/news/list";
    }

    @GetMapping("/add")
    public String add() {
        return "mall/news/add";
    }

    @GetMapping("/edit/{id}")
    public String mallNewsEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        MallNews news = mallNewsService.getById(id);
        map.addAttribute("news", news);
        return "mall/news/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(MallNews mallNews) {
        QueryWrapper<MallNews> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(mallNews.getNewsName())){
            qw.like("news_name", mallNews.getNewsName());
        }
        IPage<MallNews> page = startPage();
        IPage<MallNews> mallNewsList = mallNewsService.page(page, qw);
        return page(mallNewsList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            MallNews sysRole
    ) {
        mallNewsService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ){
        Long[] mallNewsIds = Convert.toLongArray(ids);
        mallNewsService.removeByIds(Arrays.asList(mallNewsIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(MallNews mallNews) {
        mallNewsService.changeStatus(mallNews);
        return success();
    }

}
